const storage = {
    set(key, value) {
        localStorage.setItem(key, JSON.stringify(value));
    },
    get(key) {
        const str = localStorage.getItem(key);
        return JSON.parse(str);
    },
    remove(key) {
        localStorage.removeItem(key);
    }
}
// 用户数据库
const userDB = {

    add({ username, password }) {
        const users = storage.get('users') || [];
        users.push({ username, password });
        try {
            storage.set('users', users);
        } catch (error) {
            console.log(error);
        }
    },
    include(username) {
        const users = storage.get('users') || [];
        let flag = false;
        users.forEach(item => {
            if (item.username === username) {
                flag = true;
            }
        });
        return flag
    },
    findOne(username) {
        const users = storage.get("users") || [];
        const user = users.find(item => {
            return item.username === username;
        });
        return user;
    },
    updateOne(username, user) {
        console.log(user);
        let users = storage.get("users") || [];
        users.forEach(item => {
            if (item.username === username) {
                Object.assign(item, user);
            }
        });
        console.log(users);
        storage.set("users", users);
    }
}
//pass
const parseLocationParam = function () {

}

/**
 * 
 * @param {HTMLElement} form 
 */
const getFormData = function (form) {
    const data = {};
    [...form.children].forEach(item => {
        if (item.tagName !== "INPUT") return;
        data[item.name] = item.value;
    });
    return data;
}
const clearFormData = function (form) {
    const data = {};
    [...form.children].forEach(item => {
        if (item.tagName !== "INPUT") return;
        item.value = "";
    });
}

